(function(){
	var pagenum=10;//每页数量
	var curpage=1;//当前页面
	var totalPage=0;//总页数
	//获取列表
	function getlist(curpage,pagenum){
	   $.ajax({
			url:'http://web.icecn.net/pay/getPayActivityList.json',
			type:'get',
			async:true,
			data:{
                offset:pagenum*(curpage-1),
				limit:pagenum,
			},
			xhrFields:{withCredentials:true},
			dataType: "json",
			success: function(data){
				if(data.code === 200 ){
				    dataset=data.t.result;
					if(dataset && dataset.length > 0){
						for(var i=0;i<dataset.length;i++){
						   dataset[i].startTime=dataset[i].startTime==null?"-":getMyDate(dataset[i].startTime)
						   dataset[i].overTime=dataset[i].overTime==null?"-":getMyDate(dataset[i].overTime)
						   switch(dataset[i].payClientType){
							 case 1 :{
								dataset[i].payClientType="IOS" ;
								break;
							 }
							 case 2 :{
								dataset[i].payClientType="Android" ;
								break;
							 }
							 case 3 :{
								dataset[i].payClientType="IOS,Android" ;
								break;
							 }
						   }
					   }
					   var html = template('datalist',{dataset:dataset});
					   $("#example1").html(html);
					   totalPage=data.t.totalPage;
					   $(".pageinfo").html("当前"+curpage+"页/共"+totalPage+"页");
					   $(".curpage").val(curpage);
					   if(curpage==1){$(".prev").addClass("disabled")}else{$(".prev").removeClass("disabled")}
					   if(curpage==totalPage){$(".next").addClass("disabled")}else{$(".next").removeClass("disabled")}
					}else{
						$("#example1").html("<tbody><tr><td>查不到数据</td></tr></tbody>");
					};
				}
			}
	   });
	}
	var Page = {
		init:function(){
			this.render();
			this.event();
		},
		//绑定事件
		event:function(){
			//上一页
		    $(".prev").click(
			    function(){
					if(curpage>1){
				     	curpage=curpage-1;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//下一页
		    $(".next").click(
			    function(){
					if(curpage<totalPage){
				     	curpage=curpage+1;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//页码跳转
			$(".gopage").click(
			    function(){
					if($(".curpage").val()>=1&&$(".curpage").val()<=totalPage){
				    	curpage=parseInt($(".curpage").val());
					    getlist(curpage,pagenum)
					}else{
						$(".curpage").val(curpage);
					}
			    }
			)
			//新建修改标准活动
			$(".content-wrapper").on("click", '.addupdate1', function() {
				var id=$(this).attr("data-id");
				if(id!=null){
				  var obj=$(this).parent().parent().find("td");
				  $("#modal-addupdate1 .modal-title").html("修改标准活动");
				  $("#id1").val(id);
				  $("#paymentMoney1").val(obj.eq(1).html());
				  $("#giveDiamond1").val(obj.eq(2).html());
				  $("#payMoney").val(obj.eq(3).html());
				  $("input[name=payClientType1]").prop("checked",false) 
				  if(obj.eq(7).html()=="IOS"){
				    $("input[name=payClientType1]:first").prop("checked",true)
				  }else if(obj.eq(7).html()=="Android"){
				    $("input[name=payClientType1]:last").prop("checked",true)
				  }else{
				    $("input[name=payClientType1]").prop("checked",true) 
				  }
				}else{
				  $("#modal-addupdate1 .modal-title").html("新建标准活动")
				  $("#id1").val("");
				  $("#paymentMoney1").val("");
				  $("#giveDiamond1").val("");
				  $("#payMoney").val("");
				  $("input[name=payClientType1]").prop("checked",false)
				}
			})
			//确认新建修改标准活动
			$(".btnaddupdate1").click(function(){
				 var id=$("#id1").val();
				 var paymentMoney=$("#paymentMoney1").val()
				 var giveDiamond=$("#giveDiamond1").val()
				 var payMoney=$("#payMoney").val()
				 var payClientType="";
				 $("input[name=payClientType1]").each(function() {
					if ($(this).prop("checked")) {
						payClientType +=$(this).val();
					}
			     }); 
				 if(payClientType.length>1){
					 payClientType=3
				 }
				 if(paymentMoney==""){alert("请输入充值钻石");return}
				 if(payMoney==""){alert("请输入支付金额");return}
				 if(payClientType.length==0){alert("请选择应用渠道");return}
				 if(id>0){
					 $.ajax({
						url:'http://web.icecn.net/pay/editActivity.json',
						type:'POST',
						async:true,
						data:{
							id:id,
							activityType:1,
							giveDiamond:giveDiamond,
							paymentMoney:paymentMoney,
							payClientType:payClientType,
							payMoney:payMoney
						},
					    xhrFields:{withCredentials:true},
						dataType: "json",
						success: function(data){
							if(data.code === 200 ){
								$("#modal-addupdate1").modal('hide');
								getlist(curpage,pagenum);
							}else{
								alert(data.msg)	
							}
						}
				     });
				 }else{
					 $.ajax({
						url:'http://web.icecn.net/pay/saveActivity.json',
						type:'POST',
						async:true,
						data:{
							activityType:1,
							giveDiamond:giveDiamond,
							paymentMoney:paymentMoney,
							payClientType:payClientType,
							payMoney:payMoney
						},
					    xhrFields:{withCredentials:true},
						dataType: "json",
						success: function(data){
							if(data.code === 200 ){
								$("#modal-addupdate1").modal('hide');
								getlist(curpage,pagenum);
							}else{
								alert(data.msg)	
							}
						}
				     });
				 }
			})
			//新建修改充值活动
			$(".content-wrapper").on("click", '.addupdate2', function() {
				var id=$(this).attr("data-id");
				if(id!=null){
				  var obj=$(this).parent().parent().find("td");
				  $("#modal-addupdate2 .modal-title").html("修改充值活动");
				  $("#id2").val(id);
				  $("#paymentMoney2").val(obj.eq(1).html());
				  $("#giveDiamond2").val(obj.eq(2).html());
				  $("#activityTime").val(obj.eq(6).html());
				  $("input[name=payClientType2").prop("checked",false) 
				  if(obj.eq(7).html()=="IOS"){
				    $("input[name=payClientType2]:first").prop("checked",true)
				  }else if(obj.eq(7).html()=="Android"){
				    $("input[name=payClientType2]:last").prop("checked",true)
				  }else{
				    $("input[name=payClientType2]").prop("checked",true) 
				  } 
				  var isActivity=$(this).attr("data-isActivity");
				  if(isActivity=="true"){
				    $("input[name=isActivity]").prop("checked",true) 
				  }else{
				    $("input[name=isActivity]").prop("checked",false)  
				  }
				  var activityPicture=$(this).attr("data-activityPicture");
				  $("#photoCover1").val(activityPicture);
				  $(".imgfile1").html("<img src='"+activityPicture+"'/>")
				  var startTime=obj.eq(4).html().substring(0,10)+'T'+obj.eq(4).html().substring(11,16);	
				  $("#startTime").val(startTime);
				  var overTime=obj.eq(5).html().substring(0,10)+'T'+obj.eq(5).html().substring(11,16);	
				  $("#overTime").val(overTime);
				  $("#payMoney2").val(obj.eq(3).html())
				}else{
				  $("#modal-addupdate2 .modal-title").html("新建充值活动")
				  $("#id2").val("");
				  $("#paymentMoney2").val("");
				  $("#giveDiamond2").val("");
				  $("#activityTime").val("");
				  $("input[name=payClientType2]").prop("checked",false)
				  $("input[name=isActivity]").prop("checked",false) 
				  $("#startTime").val("");
				  $("#overTime").val("");
				  $("#photoCover1").val("")
				  $("#lefile1").val("")
				  $(".imgfile1").html("")
				  $("#payMoney2").val("")
				}
			})
			//确认新建修改充值活动
			$(".btnaddupdate2").click(function(){
				 var id=$("#id2").val();
				 var paymentMoney=$("#paymentMoney2").val()
				 var giveDiamond=$("#giveDiamond2").val()
				 var payClientType="";
				 $("input[name=payClientType2]").each(function() {
					if ($(this).prop("checked")) {
						payClientType +=$(this).val();
					}
			     }); 
				 if(payClientType.length>1){
					 payClientType=3
				 }
				 var payMoney=$("#payMoney2").val()
				 var activityTime=$("#activityTime").val()
				 var isActivity=$("input[name=isActivity]").prop("checked")
				 var startTime=$('#startTime').val()==""?"":$('#startTime').val().substring(0,10)+' '+$('#startTime').val().substring(11)+':00'; 
				 var overTime=$('#overTime').val()==""?"":$('#overTime').val().substring(0,10)+' '+$('#overTime').val().substring(11)+':00'; 
				 var activityPicture=$("#photoCover1").val();
				 if(paymentMoney==""){alert("请输入充值钻石");return}
				 if(activityTime==""){alert("请输入活动次数");return}
				 if(payClientType.length==0){alert("请选择应用渠道");return}
				 if(startTime==""){alert("请输入开始时间");return}
				 if(overTime==""){alert("请输入结束时间");return}
				 if(id>0){
					 $.ajax({
						url:'http://web.icecn.net/pay/editActivity.json',
						type:'POST',
						async:true,
						data:{
							id:id,
							activityType:2,
							giveDiamond:giveDiamond,
							paymentMoney:paymentMoney,
							payClientType:payClientType,
							activityTime:activityTime,
							isActivity:isActivity,
							startTime:DateToUnix(startTime)+"000",
							overTime:DateToUnix(overTime)+"000",
							activityPicture:activityPicture,
							payMoney:payMoney
						},
					    xhrFields:{withCredentials:true},
						dataType: "json",
						success: function(data){
							if(data.code === 200 ){
								$("#modal-addupdate2").modal('hide');
								getlist(curpage,pagenum);
							}else{
								alert(data.msg)	
							}
						}
				     });
				 }else{
					 $.ajax({
						url:'http://web.icecn.net/pay/saveActivity.json',
						type:'POST',
						async:true,
						data:{
							activityType:2,
							giveDiamond:giveDiamond,
							paymentMoney:paymentMoney,
							payClientType:payClientType,
							activityTime:activityTime,
							isActivity:isActivity,
							startTime:DateToUnix(startTime)+"000",
							overTime:DateToUnix(overTime)+"000",
							activityPicture:activityPicture,
							payMoney:payMoney
						},
					    xhrFields:{withCredentials:true},
						dataType: "json",
						success: function(data){
							if(data.code === 200 ){
								$("#modal-addupdate2").modal('hide');
								getlist(curpage,pagenum);
							}else{
								alert(data.msg)	
							}
						}
				     });
				 }
			})
		},
		//渲染页面
		render:function(){
			getlist(curpage,pagenum)
		}
	};
	Page.init();
})();